// pages/bookingorder/bookingorder.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageRoot: app.globalData.imageRoot,
    myl: false,
    show: false,
    actions: [{
        name: '默认排序'
      },
      {
        name: '价格最高'
      },
      {
        name: '价格最低'
      },
      {
        name: '最新发布'
      },
      {
        name: '折扣率最低'
      }
    ],
    active: 0,
    status: 0,
    stats: ["不限","待审核", "已取消", "待评价", "已完成"],
    stats1: ["不限","待服务", "服务中", "待评价", "已完成"],
    option1: [{
        text: '无限制',
        value: 0
      },
      {
        text: '优选签约',
        value: 1
      },
      {
        text: '折扣签约',
        value: 2
      },
      {
        text: '放盘签约',
        value: 3
      },
    ],
    myl:false,
    value1: 0,
    option2: [{
        text: '无限制',
        value: 0
      },
      {
        text: '优选预约',
        value: 1
      },
      {
        text: '折扣预约',
        value: 2
      },
      {
        text: '放盘预约',
        value: 3
      },
      {
        text: '尽调预约',
        value: 4
      },
      {
        text: '好房预约',
        value: 5
      },
      {
        text: '增值预约',
        value: 6
      },
    ],
    value2: 0,
    yy: [],
    qy: [],
    page_token :0,
    identity:''
  },
  timeReset(){
    this.setData({
      page_token:0
    })
    var data = {
      page_token:0,
      page_size:10
    }
    this.api(data)
    this.selectComponent('#item2').toggle();
  },
  onReachBottom() { //上拉加载更多
    var that = this;
    var active = that.data.active;
    var type = [that.data.yy, that.data.qy];
    //检查是否还有数据可以加载
    var total_size = type[active].total_size;
    var size = type[active].results.length;
    var page_token = that.data.page_token+=1
    //如果还有，则继续加载
    if (total_size > size) {
      var data = {
        page_size:10,
        page_token:page_token
      }
      that.api(data)
      console.log(size, total_size)
      //如果没有了，则停止加载，显示没有更多内容了
    } else {
      that.setData({
        myl: true
      })
    }
  },
  search(e) {
    console.log(e.detail)
    var data = {
      title:e.detail,
      page_size: 10,
      page_token: 0
    }
    this.api(data)
  },
  areaReset(e){
    let that = this;
    that.setData({page_token:0 })
    var data = {
      page_size:10,
      page_token:0
    }
    that.api(data)
    if(that.data.active == 0){
      that.selectComponent('#item3').toggle();
    }else{
      that.selectComponent('#item6').toggle();
    }
  },
  inEnter(e) { //区域筛选
    var that = this;
    console.log(e.detail);
    var district = (e.detail.area.value=='全市'?'':e.detail.area.value);
    var city = (e.detail.city.value=='全省'?'':e.detail.city.value);
    var province = (e.detail.province.value=='全国'?'':e.detail.province.value);
    var data = {
      // ordertype: ordertype,
      province: province,
      city: city,
      district: district,
      page_size:10,
      page_token:0
    }
    that.api(data);
    if(that.data.active == 0){
      that.selectComponent('#item3').toggle();
    }else{
      that.selectComponent('#item6').toggle();
    }
  },
  onChange(event) {
    var that = this;
    that.setData({
      active: Number(event.detail.index),

    })
    Object.assign(that.data,{
      page_token:0
    })
    var data = {
      page_size: 10,
      ordertype:0,
      page_token:0
    }
    that.api(data)
  },
  yyMore(e) { //预约状态筛选
    var that = this;
    var index = that.data.active;
    console.log(that.data.status);
    var data = {
      stage: that.data.status,
      page_size: 10,
      page_token:0
    }
    that.api(data)
    that.selectComponent('#item1').toggle();
  },
  qyMore(e) { //签约状态筛选
    var that = this;
    console.log(that.data.status)
    var data = {
      oStage: that.data.status,
      page_size: 10,
      page_token:0
    }
    that.api(data)
    that.selectComponent('#item4').toggle();
  },
  orderType(e) { //订单类型筛选
    var that = this;
    var data = {
      page_size: 10,
      page_token:0
    }
    if(that.data.active == 0){
      that.setData({
        yy:[],
        page_token:0
      })
    }else{
      that.setData({
        qy:[],
        page_token:0
      })
    }
    that.data.ordertype = e.detail
    that.api(data)
  },
  api(data) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    if(that.data.ordertype) data.ordertype = that.data.ordertype;
    var active = Number(that.data.active);
    if (active == 0) {
      getApp().ports.qbyy({
       data:data
      }).then((res) => {
        var res = res.data;
        if(that.data.page_token == 0){
          res.results.forEach(item => {
            item.createTime = app.timeFormat(item.createTime,true)
            item.active = active+1
            switch (item.type) {
              case 2:
                item.banner = that.data.imageRoot + item.banner;
                break;
              case 3:
                item.banner = 'https://api.yipaiju.com/resources/images/wawa/logo1.png';
                item.title = item.clientName+' '+item.title +' ' + item.builtUpAreaSquareCentimeter+'㎡'+' '+item.intentionPriceYuan +'万'
                break;
              case 5:
                let total_budget_price_yuan = ['200万以下','200-500万','500-100万','1000万以上'];
                let house_area = ['50㎡以下','50㎡-90㎡','90㎡-144㎡',,'144㎡以上'];
                item.banner = 'https://api.yipaiju.com/resources/images/wawa/hf.png'
                item.title = item.clientName+' ' + item.province + item.district + ' ' + total_budget_price_yuan[item.totalBudgetPriceYuan] + ' ' + house_area[item.houseArea]
                break;
              case 6:
                let order_types = ['', '个人转贷',' 企业转贷'];
                let budget_price_yuan = ['50万以下','50万-150万','150万-300万','300万以上'];
                item.banner = 'https://api.yipaiju.com/resources/images/wawa/zz.jpg'
                item.title =  item.clientName+' ' + order_types[item.orderType] + ' ' + budget_price_yuan[item.budgetPriceYuan]
                break;
            }
          })
        }else{
          res.results.forEach(item => {
            item.active = active+1
            item.createTime = app.timeFormat(item.createTime,true)
            that.data.yy.results.push(item)
            switch (item.type) {
              case 2:
                item.banner = that.data.imageRoot + item.banner;
                break;
              case 3:
                item.banner = 'https://api.yipaiju.com/resources/images/wawa/logo1.png'
                break;
              case 5:
                item.banner = 'https://api.yipaiju.com/resources/images/wawa/hf.png'
                break;
              case 6:
                item.banner = 'https://api.yipaiju.com/resources/images/wawa/zz.jpg'
                break;
            }
          })
            res.results = that.data.yy.results
        }
        // that.userInfo(res)
        wx.hideLoading({
          success: (res) => {},
        })
        that.setData({
          yy: res
        })
       
      }).catch(() => {
        console.error('出错');
      });
    } else if (active == 1) {
      getApp().ports.api19({
        data: data
      }).then((res) => {
        var res = res.data; 
        if(data.page_token >= 1){
          if (that.data.Loading) {
            wx.hideLoading({
              success: (res) => {},
            })
          }
          res.results.forEach(item => {
            item.active = active+1
            item.createTime = app.timeFormat(item.createTime,true)
            if(item.type == 3) item.banner = 'https://api.yipaiju.com/resources/images/wawa/logo1.png';
            if(item.type == 2) item.banner = that.data.imageRoot + item.banner;
            that.data.qy.results.push(item);
          })
            res.results = that.data.qy.results
        }else{
          res.results.forEach(item => {
            if(item.type == 3) item.banner = 'https://api.yipaiju.com/resources/images/wawa/logo1.png';
            if(item.type == 2) item.banner = that.data.imageRoot + item.banner;
            item.active = active+1
            item.createTime = app.timeFormat(item.createTime,true)
          })
        }
        wx.hideLoading({
          success: (res) => {},
        })
        that.setData({
          qy: res
        })
        // that.userInfo(res)
      }).catch(() => {
        console.error('出错');
      });
    }

  },
  userInfo(user) {
    var that = this;
    var list = that.data.list;
    let liky = user.results.filter((p) => {
      return p.client
    })
    let v = [];
    for (let i = 0; i < liky.length; i++) {
      v[i] = liky[i].client
    }
    getApp().ports.api27({
      data: v
    }).then((res) => {
      var res = res.data;
      console.log(list)
      for (let i in user.results) {
        user.results[i].create_time = app.timeFormat(user.results[i].create_time, true);
        for (let j in res) {
          if (user.results[i].client == res[j].id) {
            user.results[i].markName = res[j].nickname;
            user.results[i].avatar_url = res[j].avatar_url;
            user.results[i].telphone = res[j].phone;
            var obj = {
              name: res[j].nickname,
              phone: res[j].phone,
              img: res[j].avatar_url,
              client:user.results[i].client,
              no:user.results[i].no,
              // create_time:user.results[i].create_time
            }
            if (that.data.active === 0) {
              if(that.data.identity === '市场专员' || that.data.identity === '合伙专员'){
                if (user.results[i].type == 1 || user.results[i].type == 2 || user.results[i].type == 4) {
                  user.results[i].url = '../yyDetails/yyDetails?typeId=' + user.results[i].follow_id + '&type=' + user.results[i].type + '&user=' + encodeURIComponent(JSON.stringify(obj))
                } else {
                  user.results[i].url = '../fYdetails/fYdetails?typeId=' + user.results[i].follow_id + '&type=' + user.results[i].type + '&user=' + encodeURIComponent(JSON.stringify(obj))
                }
              }else{
                if (user.results[i].type == 1 || user.results[i].type == 2 || user.results[i].type == 4) {
                  user.results[i].url = '../fpReserve/fpReserve?id='+user.results[i].id +'&type='+user.results[i].type
                } else {
                  user.results[i].url = '../fpyy/fpyy?id=' +user.results[i].id +'&type='+user.results[i].type
                }
              }
            } else if(that.data.active === 1){
              switch (user.results[i].type) {
                case 1:
                  user.results[i].url = '../../subpackages/pages/fpsign/fpsign?id=' + user.results[i].id
                  break;
                case 2:
                  user.results[i].url = '../../subpackages/pages/discount/discount?id=' + user.results[i].id
                  break;
                case 3:
                  if(user.results[i].stage <= 2&& (wx.getStorageSync('identity') == '市场专员' || wx.getStorageSync('identity') == '合伙专员' )  && wx.getStorageSync('userId') == user.results[i].marketer){
                    user.results[i].url = '../listing/listing?id=' + user.results[i].id
                  }else{
                    user.results[i].url = '../fplisting/fplisting?id=' + user.results[i].id
                  } 
                  break;
              }
            }
          }
        }
      }
      var keys = ['yy', 'qy'];
      var key = keys[that.data.active];
      that.setData({
        [key]: user
      })
    }).catch(() => {
      console.error('出啥错了？');
    });
  },
  areaInEnter(e) {
    var that = this;
    var ordertype = that.data.active;
    var reg = /[\u4e00-\u9fa5]/g;
    var time = e.detail.years + '-' + e.detail.months + '-' + e.detail.days;
    var create_time = time.replace(reg, "")
    var data = {
      // ordertype: ordertype,
      create_time: create_time,
      page_size: 10,
      page_token:0
    }
    that.api(data)
    if(that.data.active == 0){
      that.selectComponent('#item2').toggle();
    }else{
      that.selectComponent('#item5').toggle();
    }  
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      identity:wx.getStorageSync('identity'),
      active_color:app.globalData.active_color
    })
    var data = {
      page_size: 10,
      page_token: 0
    }
    this.api(data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page_token:0
    })
    this.api({});
    setTimeout(()=>{
      wx.stopPullDownRefresh()
      console
    },1000)
  },

 
  statbtn(e) {
    var that = this;
    console.log(e.currentTarget.dataset.index)
    var i = e.currentTarget.dataset.index;
    that.setData({
      status: i
    })
  },
  onConfirm() {
    this.selectComponent('#item').toggle();
  },

  onSwitch1Change({
    detail
  }) {
    this.setData({
      switch1: detail
    });
  },

  onSwitch2Change({
    detail
  }) {
    this.setData({
      switch2: detail
    });
  },
  Jdiction() {
    this.setData({
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  onSelect(event) {
    console.log(event.detail);
  },
  preventdefault:function(){},
})